<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\common\lib\Show;
use think\facade\Request;
use app\common\business\User as BusiUser;
use app\api\validate\User as ValiUser;

class Login extends BaseController
{
    public function index() :object
    {
        if (!Request::isPost()) {
            return Show::error('非法请求', config('status.request_action_error'));
        }

        // 获取提交数据
        $data = [
            'phone' => Request::param('phone_number', '', 'trim'),
            'code'  => Request::param('code', 0, 'intval'),
            'type'  => Request::param('type', 0, 'intval')
        ];

        // 提交数据验证
        $validate = new ValiUser();
        if (!$validate->scene('login')->check($data)) {
            return Show::error($validate->getError());
        }

        // 登录验证
        try{
            $user = new BusiUser();
            $res  = $user->login($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage(), $e->getCode());
        }

        return $res ? Show::success($res, '登陆成功') : Show::error('登陆失败');
    }
}
